package com.chuantou.yuba.db.util;

import android.database.Cursor;

/**
 * @description 游标处理工具类
 */
public class CursorHelper {

	/**
	 * 从游标中读取字符串
	 * 
	 * @param cursor
	 * @param columnName
	 * @param defaultVal
	 * @return
	 */
	public static String getString(Cursor cursor, String columnName, String defaultVal) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getString(columnIndex);
		}
		return defaultVal;
	}

	public static long getLong(Cursor cursor, String columnName, long defaultVal) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getLong(columnIndex);
		}
		return defaultVal;
	}

	public static float getFloat(Cursor cursor, String columnName, float defaultVal) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getFloat(columnIndex);
		}
		return defaultVal;
	}

	public static double getDouble(Cursor cursor, String columnName, double defaultVal) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getDouble(columnIndex);
		}
		return defaultVal;
	}

	public static int getInt(Cursor cursor, String columnName, int defaultVal) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getInt(columnIndex);
		}
		return defaultVal;
	}

	public static short getShort(Cursor cursor, String columnName, short defaultVal) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getShort(columnIndex);
		}
		return defaultVal;
	}

	public static byte[] getBlob(Cursor cursor, String columnName) {
		int columnIndex = cursor.getColumnIndex(columnName);
		if (columnIndex != -1) {
			return cursor.getBlob(columnIndex);
		}
		return null;
	}

}
